package com.example.smartcity4.ui.metro;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.MyListView;
import com.example.smartcity4.R;
import com.example.smartcity4.adapter.MetroAdapter;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.reteofi.SmartService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MetroActivity extends AppCompatActivity {
    private TextView title;
    private ImageView left;
    private MyListView myList;
    private ImageView more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metro);
        initView();
    }

    private void initView() {

        title = findViewById(R.id.title);
        left = findViewById(R.id.left);
        myList = findViewById(R.id.my_list);
        title.setText("地铁查询");
        more=findViewById(R.id.more);
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),MetroCityActivity.class));
            }
        });


        initData();
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService service = retrofit.create(SmartService.class);

        Call<GsonBean> call = service.get_metro("建国门");
        call.enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, final Response<GsonBean> response) {
                myList.setAdapter(new MetroAdapter(response.body().data, getApplicationContext()));
                myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        System.out.println(".................."+response.body().data.get(position).lineId);
                        startActivity(new Intent(getApplicationContext(), MetroDalActivity.class).putExtra("id", response.body().data.get(position).lineId));
                    }
                });
                //
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {

            }
        });


    }
}